const app = getApp();
Component({
  options:{
      multipleSlots: true, //多slot支持
      addGlobalClass: true
  },
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    screenHeight: app.globalData.screenHeight,
    selected: 0,
    color: "#8A8A8A",
    selectedColor: "#F66B1D",
    list: [{
      pagePath: "/pages/home/home",
      iconPath: "/images/tabbar/home.png",
      selectedIconPath: "/images/tabbar/homeHover.png",
      text: "首页"
    },{
      pagePath: "/pages/wantBuy/wantBuy",
      iconPath: "/images/tabbar/wantBuy.png",
      selectedIconPath: "/images/tabbar/wantBuyHover.png",
      text: "求购"
    },{
      pagePath: "/pages/addIndex/addIndex",
      iconPath: "/images/tabbar/add.png",
      selectedIconPath: "/images/tabbar/add.png",
      text: ""
    },{
      pagePath: "/pages/garage/garage",
      iconPath: "/images/tabbar/garage.png",
      selectedIconPath: "/images/tabbar/garageHover.png",
      text: "车库"
    },{
      pagePath: "/pages/me/me",
      iconPath: "/images/tabbar/me.png",
      selectedIconPath: "/images/tabbar/meHover.png",
      text: "我的"
    }],
    // hideTabbar:false,
    modalName:null,
    accountStatus: 2,
    roleList:[],
    msgNumber:{
      noticeNums:0,
      dealerUserApplyNums:0,
      totalNums:0
    },
    remindList:[],
    kefudonghua:'slide-bottom'
  },
  methods: {
    
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path;
      if(data.index == 0){
        wx.switchTab({url})
      }else{
        // 验证是否有权限
        if(2 != this.data.accountStatus){
          // this.showModal();
          this.showRegister();
        }else{
          if(data.index == 2){
            // this.subRegister();
            this.showModal({currentTarget:{dataset:{name:'showPush'}}});
          }else{
            wx.switchTab({url})
          }
        }
      }
    },
    initRegisterComponent(){
      console.log('被初始化---tabbar');
      console.log('用户信息',app.globalData.userInfo);
      let userInfo = app.globalData.userInfo;
      // 判断用户身份信息，组合对象List
      if(userInfo){

        if(2 == userInfo.userType){ // 1车商用户  2客户经理
          let newList = this.data.list;
          newList[3].text = '管理';
          newList[3].pagePath = "/pages/manage/manage";
          this.setData({
            list:newList
          });
        }
      }
      this.registerComponent = this.selectComponent('#registerComponent');
      this.registerComponent.lifeInit();
    },
    showRegister(){
      this.registerComponent.showModal();
    },
    closePush(){
      this.setData({
        'showPush': false
      })
    },
    showModal(e){
      let name = e.currentTarget.dataset.name;
      if( name == 'showPush' ){
        this.setData({
          'modalName': name,
          'kefudonghua': 'slide-bottom'
        });
      }
      let self = this;
      setTimeout(() => {
        self.setData({
          'hideTabbar': true
        })
      }, 190);
      setTimeout(() => {
        self.setData({
          'kefudonghua': 'small-shake'
        })
      }, 1600);
    },
    hideTabbar(){
      this.setData({
        'hideTabbar': true
      })
    },
    showTabbar(){
      this.setData({
        'hideTabbar': false
      })
    },
    hideModal(){
      this.setData({
        'modalName': null,
        'hideTabbar': false
      })
    },  
    // 跳转到发布自有车辆页面
    jumpReleaseToMySelf(){
      if (this.isHaveAuth('addCar')) {
        let self = this;
        wx.navigateTo({
          url: '/pages/release/release',
          success: function(res) {
            self.hideModal();
          }
        })
      }else{
        app.error('您无权操作');
      }
    },
    // 跳转到发布拍卖页面
    jumpReleaseToAuction(){
      if (this.isHaveAuth('manageAuction')) {
        let self = this;
        wx.navigateTo({
          url: '/pages/release/auction/auction',
          success: function(res) {
            self.hideModal();
          }
        })
      }else{
        app.error('您无权操作');
      }
    },
    // 跳转到发布求购页面
    jumpReleaseToWantBuy(){
      if (this.isHaveAuth('addCar')) {
        let self = this;
        wx.navigateTo({
          url: '/pages/release/wantBuy/wantBuy',
          success: function(res) {
            self.hideModal();
          }
        })
      }else{
        app.error('您无权操作');
      }
    },
    // 跳转到查询维保的地方
    jumpMaintenance(){
      let self = this;
      wx.navigateTo({
        url: '/pages/maintenance/maintenance',
        success: function(res) {
          self.hideModal();
        }
      })
      // if (this.isHaveAuth('reportApplyAudit')) {
      // }else{
      //   app.error('您无权操作');
      // }
    },
    isHaveAuth(authIcon){
      let index = this.data.roleList.indexOf(authIcon);
      return index != -1?true:false;
    },
    subRegister(){
      var pages = getCurrentPages();//获取当前页面栈
      if (pages.length > 0) {  
        var currPage = pages[pages.length - 1]; // 当前页面，若不对当前页面进行操作，可省去
        // console.log('是否获取到页面堆栈',currPage);
        currPage.lifeInit();//触发上一个页面中的方法
      }
      // wx.startPullDownRefresh();
    },
    openCustomerServiceChat(){
      this.hideModal();
      app.openCustomerServiceChat();
    },
  },
  attached:function(){
    let userInfo = app.globalData.userInfo;
    if(userInfo){
      if(2 == userInfo.userType){ // 1车商用户  2客户经理
        let newList = this.data.list;
        newList[3].text = '管理';
        newList[3].pagePath = "/pages/manage/manage";
        this.setData({
          list:newList
        });
      }
    }
  },
})